// 导入axios
import axios from 'axios'

export default {
  namespaced: true,
  state: () => ({
    token: '',
    userInfo: {}
    // token: localStorage.getItem('token') || ''
  }),
  mutations: {
    // 定义一个修改token的mutation函数
    updateToken (state, token) {
      state.token = token
      // 每次修改都存入localStorage
    //   localStorage.setItem('token', token)
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async getUserInfo (context) {
      // console.log(context)
      // 发起ajax请求
      // const { data: res } = await axios.get('/my/userinfo', {
      //   headers: {
      //     Authorization: context.state.token
      //   }
      // })
      // console.log(res)
      // 加入请求拦截器后不需要传入token
      const { data: res } = await axios.get('/my/userinfo')
      context.commit('updateUserInfo', res.data)
    }
  }
}
